package cn.doitedu.rtmk.web;

import org.junit.Test;
import org.roaringbitmap.longlong.Roaring64Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;

/**
 * @Author: 深似海
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2023/12/24
 * @Desc: 学大数据，上多易教育
 *   规则管理平台的后端模拟器
 **/
public class RuleService {

    @Test
    public void insertModelA_rule001() throws IOException, SQLException {
        // 从前端页面收到营销人员定义的规则参数
        String ruleParamJson = "{\n" +
                "  \"rule_id\": \"rule-A-001\",\n" +
                "  \"dynamic_profile_condition1\": {\n" +
                "    \"event_id\": \"R\",\n" +
                "    \"operator\": \">\",\n" +
                "    \"count\": 6\n" +
                "  },\n" +
                "  \"dynamic_profile_condition2\": {\n" +
                "    \"event_id\": \"K\",\n" +
                "    \"property_name\": \"p2\",\n" +
                "    \"operator\": \">\",\n" +
                "    \"max_min_bs\": 8\n" +
                "  },\n" +
                "  \"trigger_condition\": {\n" +
                "    \"event_id\": \"M\",\n" +
                "    \"property_name\": \"p1\",\n" +
                "    \"operator\": \">\",\n" +
                "    \"compare_value\": 2\n" +
                "  }\n" +
                "}";

        // 查询es，圈选出满足规则静态画像条件的人群
        // 假装查完了，得到如下满足条件的用户
        HashSet<Long> preSelectUsers = new HashSet<>(Arrays.asList(1L,3L,5L,6L,8L,9L));
        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf();
        for (Long uid : preSelectUsers) {
            bitmap.add(uid);
        }
        // 序列化bitmap成byte[]
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
        bitmap.serialize(dataOutputStream);
        byte[] bitmapBytes = byteArrayOutputStream.toByteArray();


        // 生成规则元数据，写入规则元数据表
        Connection connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit43", "root", "root");
        // 准备sql模板
        PreparedStatement preparedStatement = connection.prepareStatement("insert into rule_meta values (?,?,?,?,?,?)");
        // 填充value
        preparedStatement.setString(1,"rule-A-001");
        preparedStatement.setString(2,"A");
        preparedStatement.setString(3,"注册促单");
        preparedStatement.setString(4,"新用户注册后，用优惠券诱骗它下单");
        preparedStatement.setString(5,ruleParamJson);
        preparedStatement.setBytes(6,bitmapBytes);
        // 执行sql
        preparedStatement.execute();

        preparedStatement.close();
        connection.close();


    }

    @Test
    public void insertModelA_rule002() throws IOException, SQLException {
        // 从前端页面收到营销人员定义的规则参数
        String ruleParamJson = "{\n" +
                "  \"rule_id\": \"rule-A-002\",\n" +
                "  \"dynamic_profile_condition1\": {\n" +
                "    \"event_id\": \"X\",\n" +
                "    \"operator\": \">\",\n" +
                "    \"count\": 2\n" +
                "  },\n" +
                "  \"dynamic_profile_condition2\": {\n" +
                "    \"event_id\": \"G\",\n" +
                "    \"property_name\": \"p1\",\n" +
                "    \"operator\": \">\",\n" +
                "    \"max_min_bs\": 2\n" +
                "  },\n" +
                "  \"trigger_condition\": {\n" +
                "    \"event_id\": \"H\",\n" +
                "    \"property_name\": \"p1\",\n" +
                "    \"operator\": \">\",\n" +
                "    \"compare_value\": 3\n" +
                "  }\n" +
                "}";

        // 查询es，圈选出满足规则静态画像条件的人群
        // 假装查完了，得到如下满足条件的用户
        HashSet<Long> preSelectUsers = new HashSet<>(Arrays.asList(1L,3L,5L,9L,10L,11L,12L));
        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf();
        for (Long uid : preSelectUsers) {
            bitmap.add(uid);
        }
        // 序列化bitmap成byte[]
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
        bitmap.serialize(dataOutputStream);
        byte[] bitmapBytes = byteArrayOutputStream.toByteArray();


        // 生成规则元数据，写入规则元数据表
        Connection connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit43", "root", "root");
        // 准备sql模板
        PreparedStatement preparedStatement = connection.prepareStatement("insert into rule_meta values (?,?,?,?,?,?)");
        // 填充value
        preparedStatement.setString(1,"rule-A-002");
        preparedStatement.setString(2,"A");
        preparedStatement.setString(3,"七夕促单");
        preparedStatement.setString(4,"用一点小恩小惠小浪漫，骗那些小青年疯狂消费");
        preparedStatement.setString(5,ruleParamJson);
        preparedStatement.setBytes(6,bitmapBytes);
        // 执行sql
        preparedStatement.execute();

        preparedStatement.close();
        connection.close();


    }


    @Test
    public void insertModelB_rule001() throws IOException, SQLException {
        // 从前端页面收到营销人员定义的规则参数
        String ruleParamJson = "{\n" +
                "  \"rule_id\":\"rule-B-001\",\n" +
                "  \"model_id\":\"B\",\n" +
                "  \"static_profile_conditions\":[\n" +
                "    {\n" +
                "      \"tag_name\":\"age\",\n" +
                "      \"operator\":\">\",\n" +
                "      \"compare_value\":18\n" +
                "    },\n" +
                "    {\n" +
                "      \"tag_name\":\"gender\",\n" +
                "      \"operator\":\"=\",\n" +
                "      \"compare_value\":\"male\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"tag_name\":\"tag103\",\n" +
                "      \"operator\":\">\",\n" +
                "      \"compare_value\":300\n" +
                "    }\n" +
                "  ],\n" +
                "  \"dynamic_profile_condition\":{\n" +
                "    \"event_id\":\"X\",\n" +
                "    \"operator\":\">=\",\n" +
                "    \"compare_value\":5\n" +
                "  },\n" +
                "  \"trigger_event_id\":\"V\"\n" +
                "}\n";

        // 查询es，圈选出满足规则静态画像条件的人群
        // 假装查完了，得到如下满足条件的用户
        HashSet<Long> preSelectUsers = new HashSet<>(Arrays.asList(1L,2L,3L,4L,5L));
        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf();
        for (Long uid : preSelectUsers) {
            bitmap.add(uid);
        }
        // 序列化bitmap成byte[]
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
        bitmap.serialize(dataOutputStream);
        byte[] bitmapBytes = byteArrayOutputStream.toByteArray();


        // 生成规则元数据，写入规则元数据表
        Connection connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit43", "root", "root");
        // 准备sql模板
        PreparedStatement preparedStatement = connection.prepareStatement("insert into rule_meta values (?,?,?,?,?,?)");
        // 填充value
        preparedStatement.setString(1,"rule-B-001");
        preparedStatement.setString(2,"B");
        preparedStatement.setString(3,"多易新年报名优惠促销");
        preparedStatement.setString(4,"用江哥的颜值和实力征服你");
        preparedStatement.setString(5,ruleParamJson);
        preparedStatement.setBytes(6,bitmapBytes);
        // 执行sql
        preparedStatement.execute();

        preparedStatement.close();
        connection.close();


    }



}
